<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class SignupController extends MY_Controller {

	function __construct() {
		parent::__construct();
	}

	/**
	 * 
	 */
	public function index() {
		//Validação apra redirecionar para profile caso esteja logado
		if(Auth::is_auth($this->session))
			redirect(url(URL_DEFAULT_LOGGED));
		
		if($this->is_post()) {
			//$error = new ErrorVo();
			
			//validação dos campos.
			$rules = array(
					array(
							'field' => 'user_email',
							'label' => '',
							'rules' => 'trim|xss_clean'
					),
					array(
							'field' => 'user_name',
							'label' => '',
							'rules' => 'trim|xss_clean'
					),
					array(
							'field' => 'user_password',
							'label' => '',
							'rules' => 'trim|xss_clean'
					),
					array(
							'field' => 'country_id',
							'label' => '',
							'rules' => 'trim|xss_clean'
					)
			);
				
			$validate = $this->form_validation->validate_data( $rules );
			//Verificaa validação do formulario
				
			if( $validate[ 'status' ] ) {
				$form = $validate['data'];
				
				$insert = array(
					'user_token' => create_guid('urs'),
					'user_email' => $form['user_email'],
					'user_name' => $form['user_name'],
					'country_id' => $form['country_id'],
					'user_password' => md5($form['user_password']),
					'user_create' => cur_date()
				);
				
				//Insert
				if($this->model->insert_user($insert)) {
					echo "Cadastro criado com sucesso";
				}
				else {
					echo "deu erro na hora de criar";
				}
			}
			else {
				echo FORM_MESSAGE_ERROR_VALIDADE;
			}
		}
		
		//Recupera paises
		$countries = $this->shared_model->get_countries(cur_lang(user_lang()));
		
		$this->set_data('title', PAGE_TITLE);
		$this->set_data('description', PAGE_DESCRIPTION);
		$this->set_data('countries', $countries);
		
		$this->load_view('signup/index');
	}	
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */